package com.example.cloud.exp;

import com.example.cloud.resp.ResultData;
import com.example.cloud.resp.ReturnCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2024-03-06
 * Time: 21:02
 */

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 返回给客户端对应的异常
    public ResultData<String> exception(Exception e) {
        log.error("exception:{}", e.getMessage());
        return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
    }


//    @ExceptionHandler(Exception.class)
//    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
//    public ResultData<String> exceptionAll(Exception e) {
//        log.error("exception:{}", e.getMessage());
//        return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage() + "服务器内部错误");
//    }
}
